home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files For Lab 8 / EchoNums < prev    next >
Text File  |  1995-07-13  |  5KB  |  100 lines

  1. ; This program illustrates the I/O capabilities of xComputer.
  2. ; To use this program, you must turn on the "Use I/O Services"
  3. ; option in the "Options" menu.  (If this option is off, the
  4. ; computer will not recognize the I/O instructions gtc and ptc
  5. ; when the program is run.)
  6.  
  7. ; The program reads integers input by the user.  An integer
  8. ; can consist of several digits, such as "123".  The user types
  9. ; these digits and the program keeps track of the number typed.
  10. ; The user indicates the end of a number by typeing a ".".
  11. ; When the computer reads the ".", it echos the number to output.
  12. ; This continues until the user presses return.  At that point,
  13. ; the program ends.  Aside from digits ("0" through "9"), periods,
  14. ; and carriage returns, all other characters are ignored.
  15.  
  16. ; (Note that if the user types in too many digits to be represented
  17. ; by the xComputer, the output number will be incorrect.  Numbers
  18. ; on the xComputer are limited to 16 bits.)
  19.  
  20.  
  21.            lod-c 0        ; "Num" holds the number being input.
  22.            sto num        ; Start with Num = 0, before anything is typed.
  23.  
  24. get_ch:    gtc            ; Main loop:  Get the next char from input.
  25.            jmz get_ch
  26.  
  27.            sto ch         ; Save the non-zero char in "ch" for processing.
  28.                           ;    Note that it is actually the ASCII code of
  29.                           ;    ch that is stored.
  30.  
  31.            lod ch         ; Compare the input char to "carriage return", and 
  32.            sub-c 13       ;     jump to "done" if ch is a carriage return.
  33.            jmz done
  34.  
  35.            lod ch         ; Compare the input char to ".".  If it is ".",
  36.            sub-c '.       ;     then jump to "end_num", where the num will
  37.            jmz end_num    ;     be output.
  38.  
  39.                           ; The next six instructions check whether the
  40.                           ; input character is in the range from "0" to
  41.                           ; "9".  If not, the program ignores the char
  42.                           ; and jumps back to get_ch to get the next input
  43.                           ; character.
  44.  
  45.            lod-c '9       ; Check if ch if bigger than "9" by subtracting
  46.            sub ch         ;    ch from the ASCII code of "9".  If the result
  47.            jmn get_ch     ;    is negative, then ch is > "9", so ignore it
  48.                           ;    and go back to get the next char.
  49.  
  50.            lod ch         ; Check if ch is less than "0" by subtracting
  51.            sub-c '0       ;    the ASCII code of "0" from ch.  If the result
  52.            jmn get_ch     ;    is negative, then ch < "0", so ignore it
  53.                           ;    and go back to get the next char.
  54.  
  55.            sto digit      ; After subtracting the ASCII code of "0" from
  56.                           ;    ch, the number in the accumulator happens
  57.                           ;    to be the actual number represented by the
  58.                           ;    character ch.  (If ch is "0", the number is
  59.                           ;    0; if ch is "1", the number is 1; etc.  This
  60.                           ;    single-digit number is stored in memory
  61.                           ;    location "digit".
  62.  
  63.                           ; The next 8 instructions adjust the value stored
  64.                           ; in location "num" to reflect the new digit just
  65.                           ; typed in by the user.  The object is to compute
  66.                           ; 10 * num + digit, and to store the result back
  67.                           ; into num.  (For example, if num is "12" and the
  68.                           ; user types in a 3, then num becomes 10 * 12 + 3,
  69.                           ; or 123.)  The programming here is a bit tricky.
  70.  
  71.            lod num        ; Get the original value of num.  (Call it N.)
  72.            shl            ; Shifting left multiplies N by 2.
  73.            sto num        ; Sets num = 2*N.
  74.            shl            ; Shifting left again sets the accumulator = 4*N.
  75.            shl            ; Shifting left again sets the accumulator = 8*N.
  76.            add num        ; Adds 2*N to 8*N, giving 10*N in the accumulator.
  77.            add digit      ; Adds digit to 10*N.
  78.            sto num        ; Stores (18*N + digit) back into num.
  79.            jmp get_ch     ; Go back to process the nest input char.
  80.  
  81. end_num:   lod num        ; The program jumps here when the user types ".".
  82.            pti            ;    Get the value of num and write it to output
  83.                           ;    as an integer.
  84.  
  85.            lod-c 0        ; Start over with num = 0.
  86.            sto num
  87.            jmp get_ch     ; Go back to read and process the next character.
  88.  
  89. done:      hlt            ; The program jumps here when the user types
  90.                           ;     a carriage return.  The program ends.
  91.  
  92. ch:        data           ; Location to hold character input by user.
  93.  
  94. digit:     data           ; Location to hold the single-digit number
  95.                           ;   represented by an input character in the
  96.                           ;   range "0" through "9".
  97.  
  98. num:       data           ; Location to hold the (multi-digit) number
  99.                           ;   begin typed in by the user.
  100.